package hn.cch.util;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ExcelUtil
{
    private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);

    public enum EXCEL{
        XLS(".xls"),
        XLSX(".xlsx");

        public String excel;

        EXCEL(String excel) {
            this.excel = excel;
        }
    }






    public File excelFile(String path, String name, EXCEL type){
        File file = new File(path + name + type.excel);
        return file;
    }

    // public cellFont(){
    //     HSSFFont
    //
    // }

    public boolean readExcel(String path, String name, String type){

        return true;
    }


    public boolean writeExcel(File excelFile, String tableName , String tableDesc, String[][] tableHead, Object[][] tableBody, String tableFoot) throws IOException {

        //区分xls和xlsx
        Workbook workbook;
        Sheet sheet;
        Row row;
        Cell cell;
        Font font;

        CellStyle cellStyle;

        // 工作簿
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(StreamUtil.toInput(excelFile));
        // 表格
        HSSFSheet hssfSheet = hssfWorkbook.createSheet(excelFile.getName());

        // 表头
        HSSFRow hssfRow = hssfSheet.createRow(0);
        HSSFCell hssfCell = hssfRow.createCell(0);


        // HSSFCellStyle hssfCellStyle = new HSSFCellStyle();
        // hssfCell.setCellStyle();


        return true;

    }



    // XSSF


    /**
     * 以行列为键转化为键值对
     * @param sheet
     */
    public static Map<String, String> readSheet(Sheet sheet){
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {//行
            Row row = sheet.getRow(i);
            for (int j = 0; j < row.getLastCellNum(); j++) {//列
                Cell cell = row.getCell(j);
                String key = i + "_" + j;
                if (cell == null){
                    map.put(key, null);
                }else {
                    String value = cell.getStringCellValue();
                    if ("".equals(value.trim())){

                    }else {
                        map.put(key, value);
                    }

                }
            }
        }
        return map;
    }


    public static Workbook newWorkbook(File file){
        Workbook workbook = null;
        String fileName = file.getName();
        if (fileName.endsWith(EXCEL.XLS.excel)){
            try {
                workbook = new HSSFWorkbook(new FileInputStream(file));
            } catch (IOException e) {
                logger.error("excel error");
                e.printStackTrace();
            }
        }else if (fileName.endsWith(EXCEL.XLSX.excel)){
            try {
                workbook = new XSSFWorkbook(file);
            } catch (IOException e) {
                logger.error("excel error");
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                logger.error("excel error");
                e.printStackTrace();
            }
        }else {
            logger.error("excel error");
        }
        return workbook;
    }

    public static void main(String[] args) {
        File file = new File("C:\\Users\\CCH\\Desktop\\test.xlsx");
        System.out.println(file.getName());

        Workbook workbook = newWorkbook(file);
        Sheet sheet = workbook.getSheetAt(0);
        Map<String, String> map = readSheet(sheet);

        System.out.println(map);

    }


}
